import { observable, action } from 'mobx';
import _ from 'lodash';

export interface TreeBaseStoreProps {
    treeName: string;
    treeRemark: string;
    rootName: string;
    rootIcon: string;
    defaultAdress: string;
    expandFirstNode: number;
    isRefreshTree: string;
    rightLoading: boolean;
    datas: any[];
    urlMap: any;
    query: any;
    params: any;
    clear: Function;
    update: Function;
    isInit: boolean;
    hasQuickSearch: boolean;
    expandKeys: string[];
    selectedNodeKeys: string[];
    isCube: boolean;
    isCard: boolean;
    isSearch: boolean;
    isTree: boolean;
    isReply: boolean;
    isInTree: boolean;
    windowTitle: string;
}

export default class TreeBaseStore implements TreeBaseStoreProps {

    @observable treeName = '';
    @observable treeRemark = '';
    @observable rootName = '';
    @observable rootIcon = '';
    @observable defaultAdress = '';
    @observable expandFirstNode = 0;
    @observable rightLoading = true;
    @observable datas = [] as any[];
    @observable urlMap = {} as any;
    @observable isInit = false;
    @observable hasQuickSearch = false;
    @observable expandKeys = [] as string[];
    @observable selectedNodeKeys = [] as string[];
    @observable params = {};
    @observable query = {};
    @observable isCube: boolean = false;
    @observable isCard: boolean = false;
    @observable isSearch: boolean = false;
    @observable isTree: boolean = false;
    @observable isReply: boolean = false;
    @observable windowTitle = '';
    @observable isInTree: boolean = false;
    @observable combintreeway = '0';
    @observable combintree = [];
    @observable currentTree = '';
    @observable defaulthideleft;
    @observable displaywidth:number=310;
    isRefreshTree = '0';




    @action
    update = (param) => {
        for (let key in param) {
            this[key] = param[key]
        }
    }
    @action
    updateTreeDatas = ({ pid }, datas) => {
        this.datas = this.updateChildren(pid, this.datas, datas);
    }

    @action
    addParentId2Node = (pids, datas) => {
        return datas.map(d => {
            return { ...d, pids: pids };
        });
    }

    @action
    updateChildren = (pid, datas, childs) => {
        return datas.map(d => {
            let _d = { ...d };
            if (d.domid == pid) {
                _d.childs = this.mergeChildren(d.childs, childs);
            } else {
                if (d.childs) {
                    _d.childs = this.updateChildren(pid, d.childs, childs);
                }
            }
            return _d;
        })
    }
    mergeChildren = (_childs: any[], childs: any[]) => {
        if (!_childs || _childs.length == 0) {
            return childs;
        }
        if (!childs || childs.length == 0) {
            return _childs;
        }
        return childs.map(c => {
            const newC = { ...c }
            const index = _.findIndex(_childs, (_c) => _c.domid == c.domid);
            if (index >= 0) {
                newC.childs = this.mergeChildren(_childs[index].childs, newC.childs)
            }
            return newC;
        })
    }
    @action
    clear = () => {
        this.expandKeys = [];
        this.selectedNodeKeys = [];
        this.treeName = '';
        this.treeRemark = '';
        this.rootName = '';
        this.rootIcon = '';
        this.defaultAdress = '';
        this.expandFirstNode = 0;
        this.datas = [];
        this.urlMap = {};
        this.isInit = false;
    }

}